home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Encoder / •Sources / GridEncode.cp < prev    next >
Encoding:
Text File  |  1997-04-02  |  8.4 KB  |  289 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    GridEncode.cp
  4. #
  5. #   This is just a very simple encoder that shows how to build GRID and TILE resources.
  6. #    The TILE resource is a set of 32x32x1 byte tiles.  This encoder takes a picture and
  7. #   dices it into 32x32 tiles, which it writes out to output file.  It then builds a
  8. #   standard grid, repeating the original picture as a pattern across the grid.
  9. #
  10. #   Eventually, it would be nice to make a program that lets someone graphically edit
  11. #   the available list of tiles and the actual GRID resource, much like any number
  12. #   of map editors out for various games.
  13. #
  14. #    Author: Timothy Carroll
  15. #    Apple Developer Technical Support
  16. #    timc@apple.com
  17. #
  18. #    Modification History: 
  19. #
  20. #    8/15/96        TMC     Initial Release
  21. #
  22. #    2/24/97        TMC        Now explicitly include main.h
  23. #
  24. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  25. #
  26. #
  27. #    You may incorporate this sample code into your applications without
  28. #    restriction, though the sample code has been provided "AS IS" and the
  29. #    responsibility for its operation is 100% yours.  However, what you are
  30. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  31. #    after having made changes. If you're going to re-distribute the source,
  32. #    we require that you make it clear in the source that the code was
  33. #    descended from Apple Sample Code, but that you've made changes.
  34. #
  35. *************************************************************************************/
  36.  
  37. #include "Main.h"
  38. #include "GridEncode.h"
  39. #include "GridTilesFormat.h"
  40.  
  41.  
  42. OSStatus GridTileEncode (short inputFileResNum, short outputFileResNum)
  43. {
  44.     OSStatus        theErr;
  45.     short            saveResNum;
  46.     
  47.     // Used to compile the tile resource
  48.     PicHandle        tilePict = NULL;
  49.     GWorldPtr        tileWorld = NULL;
  50.     PixMapHandle    tilePix = NULL;
  51.     
  52.     CGrafPtr         savePort;
  53.     GDHandle         saveDevice;
  54.     
  55.     Rect            pictRect;
  56.  
  57.     long            width, height, numTiles;
  58.     long            tileRowBytes;
  59.     Ptr                tileBaseAddress;
  60.     
  61.     long            x, y, loop;
  62.     
  63.     Ptr                destPtr, srcPtr;
  64.     CellGridType    *gridPtr;
  65.     CellGridType    currentValue;
  66.     
  67.     // Our output handles
  68.     Handle            tile = NULL;
  69.     Handle            grid = NULL;
  70.     
  71.     // Whether or not we've written it out as a resource
  72.     Boolean            tileIsResource = false, gridIsResource = false;
  73.         
  74.     saveResNum = CurResFile();
  75.  
  76.     UseResFile (inputFileResNum);
  77.  
  78.  
  79.     // First thing is to copy the color table resource to the output.
  80.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  81.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  82.  
  83.     // Load the color table and the graphic we want to dice into pieces
  84.     gAppColorTable = GetCTable( kAppColorTableResID );
  85.     FAIL_NIL (gAppColorTable, "\pFailed to load the color table")
  86.     
  87.     tilePict = (PicHandle) Get1Resource ('PICT', kPICTInputResID);
  88.     theErr = ResError();
  89.     FAIL_OSERR (theErr, "\pFailed to load the pict")
  90.     FAIL_NIL (tilePict, "\pFailed to load the pict")
  91.  
  92.  
  93.     
  94.     // Create a GWorld and draw our PICT into it.
  95.     
  96.     pictRect = (**tilePict).picFrame;
  97.     
  98.     pictRect.right -= pictRect.left;
  99.     pictRect.left = 0;
  100.     pictRect.bottom -= pictRect.top;
  101.     pictRect.top = 0;
  102.     
  103.     theErr = NewGWorld(&tileWorld, kPreferredDepth, &pictRect, gAppColorTable, NULL, keepLocal);
  104.     FAIL_OSERR (theErr, "\pCouldn't allocate GWorld for encoding")
  105.     FAIL_NIL (tileWorld, "\pCouldn't allocate GWorld for encoding")
  106.     
  107.     tilePix  = GetGWorldPixMap(tileWorld);
  108.     FAIL_NIL (tilePix, "\pCouldn't get the GWorld's PixMap")
  109.     FAIL_FALSE ( LockPixels(tilePix), "\pCouldn't lock GWorld PixMap")
  110.     
  111.     GetGWorld (&savePort, &saveDevice);
  112.     SetGWorld (tileWorld, NULL);
  113.     
  114.     EraseRect (&pictRect);
  115.     DrawPicture (tilePict, &pictRect);
  116.     
  117.     SetGWorld (savePort, saveDevice);
  118.  
  119.     // We're done with the picture, release it
  120.     ReleaseResource ((Handle) tilePict);
  121.     tilePict = NULL;
  122.  
  123.     // Time to build the tile resource.  We're going to dice it into as many
  124.     // 32x32 tiles as we can.  Any partial data to the bottom or the right will
  125.     // be ignored -- we only build complete tiles.
  126.     
  127.     width = pictRect.right / 32;
  128.     height = pictRect.bottom / 32;
  129.     numTiles = width * height + 1;  // Our first tile is always an all black tile, for the "borders"
  130.     
  131.     tile = NewHandleClear (sizeof (TileCollectionResHeader) + numTiles*kTileSize);
  132.     theErr = MemError();
  133.     FAIL_OSERR (theErr, "\p Failed to create the tile resource file")
  134.     FAIL_NIL (tile, "\p Failed to create the tile resource file")
  135.  
  136.     // Fill in the tile header.
  137.     (**((TileCollectionResHeader **) tile)).version = 0;
  138.     (**((TileCollectionResHeader **) tile)).depth = kPreferredDepth;
  139.     (**((TileCollectionResHeader **) tile)).flags = 0;
  140.     (**((TileCollectionResHeader **) tile)).numTiles = numTiles;
  141.     
  142.     tileBaseAddress = ( Ptr) GetPixBaseAddr( tilePix );
  143.     tileRowBytes = ( **tilePix ).rowBytes & 0x3fff;
  144.     
  145.     destPtr = (*tile) + sizeof (TileCollectionResHeader);
  146.     
  147.     // First tile, we just fill with black
  148.     for (loop = 0; loop < 32; loop++)
  149.     {
  150.         ((UInt32 *) destPtr)[0] = 0xFFFFFFFF;
  151.         ((UInt32 *) destPtr)[1] = 0xFFFFFFFF;
  152.         ((UInt32 *) destPtr)[2] = 0xFFFFFFFF;
  153.         ((UInt32 *) destPtr)[3] = 0xFFFFFFFF;
  154.         ((UInt32 *) destPtr)[4] = 0xFFFFFFFF;
  155.         ((UInt32 *) destPtr)[5] = 0xFFFFFFFF;
  156.         ((UInt32 *) destPtr)[6] = 0xFFFFFFFF;
  157.         ((UInt32 *) destPtr)[7] = 0xFFFFFFFF;
  158.         destPtr += 32;
  159.     }
  160.  
  161.     // Iterate over each tile and copy 1K of data to the destination
  162.     for (x = 0; x < width; x++)
  163.     {
  164.         for (y = 0; y < height; y++)
  165.         {
  166.             srcPtr = tileBaseAddress + 32*y*tileRowBytes + 32*x;
  167.             for (loop = 0; loop < 32; loop++)
  168.             {
  169.                 register UInt32 temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8;
  170.                 temp1 = ((UInt32 *) srcPtr)[0];
  171.                 temp2 = ((UInt32 *) srcPtr)[1];
  172.                 temp3 = ((UInt32 *) srcPtr)[2];
  173.                 temp4 = ((UInt32 *) srcPtr)[3];
  174.                 temp5 = ((UInt32 *) srcPtr)[4];
  175.                 temp6 = ((UInt32 *) srcPtr)[5];
  176.                 temp7 = ((UInt32 *) srcPtr)[6];
  177.                 temp8 = ((UInt32 *) srcPtr)[7];
  178.                 ((UInt32 *) destPtr)[0] = temp1;
  179.                 ((UInt32 *) destPtr)[1] = temp2;
  180.                 ((UInt32 *) destPtr)[2] = temp3;
  181.                 ((UInt32 *) destPtr)[3] = temp4;
  182.                 ((UInt32 *) destPtr)[4] = temp5;
  183.                 ((UInt32 *) destPtr)[5] = temp6;
  184.                 ((UInt32 *) destPtr)[6] = temp7;
  185.                 ((UInt32 *) destPtr)[7] = temp8;
  186.                 srcPtr += tileRowBytes;
  187.                 destPtr += 32;
  188.             }
  189.         }
  190.     }
  191.     
  192.     // Tile resource is done.  Dispose of the GWorld
  193.     DisposeGWorld ( tileWorld);
  194.     tileWorld = NULL;
  195.     
  196.     
  197.     // Time to create the GRID resource
  198.     grid = NewHandle (sizeof (TileGridResHeader) + 
  199.                             sizeof (CellGridType)*kGridWidth*kGridHeight);
  200.     theErr = MemError();
  201.     FAIL_OSERR (theErr, "\p Failed to allocate memory for GRID resource")
  202.     FAIL_NIL (grid, "\p Failed to allocate memory for GRID resource")
  203.     
  204.     // Fill in the header
  205.     (**((TileGridResHeader **) grid)).version = 0;
  206.     (**((TileGridResHeader **) grid)).flags = 0;
  207.     (**((TileGridResHeader **) grid)).tileResID = kOutputResID;
  208.     (**((TileGridResHeader **) grid)).width = kGridWidth;
  209.     (**((TileGridResHeader **) grid)).height = kGridHeight;
  210.     (**((TileGridResHeader **) grid)).defaultTile = 0;  // our black tile
  211.     
  212.     // Point to the start of the grid data.
  213.     gridPtr = (CellGridType * ) ((Ptr)(*grid) + sizeof (TileGridResHeader));
  214.     
  215.     // Fill in each tile
  216.     for (x = 0; x < kGridWidth; x++)
  217.     {
  218.         for (y = 0; y < kGridWidth; y++)
  219.         {
  220.             currentValue = (y % height)*height + (x % width) +1;
  221.             *gridPtr = currentValue;
  222.             gridPtr++;
  223.         }
  224.     }
  225.  
  226.     // Write the resources out to the output file.
  227.     UseResFile (outputFileResNum);
  228.     
  229.     AddResource( tile, TileCollectionResType, kOutputResID, "\p" );
  230.     theErr = ResError();
  231.     FAIL_OSERR (theErr, "\pFailed to add the TILE resource to the file")
  232.     tileIsResource = true;  
  233.     
  234.     WriteResource( tile );
  235.     theErr = ResError();
  236.     FAIL_OSERR (theErr, "\pFailed to write the TILE resource to the file")
  237.     
  238.     ReleaseResource( tile );
  239.     tile = NULL;
  240.  
  241.     AddResource( grid, TileGridResType, kOutputResID, "\p" );
  242.     theErr = ResError();
  243.     FAIL_OSERR (theErr, "\pFailed to add the GRID resource to the file")
  244.     gridIsResource = true;
  245.     
  246.     WriteResource( grid );
  247.     FAIL_OSERR (theErr, "\pFailed to write the GRID resource to the file")
  248.     theErr = ResError();
  249.     
  250.     ReleaseResource( grid );
  251.     grid = NULL;
  252.  
  253.         
  254.     // Build the grid resource;
  255.     
  256.     goto cleanup;
  257.     
  258.     error:
  259.     if (theErr == noErr)
  260.         theErr = paramErr;
  261.         
  262.     cleanup:
  263.     
  264.     UseResFile (saveResNum);
  265.     
  266.     if (tilePict != NULL)
  267.         ReleaseResource ((Handle) tilePict);
  268.         
  269.     if (tileWorld != NULL)
  270.         DisposeGWorld (tileWorld);
  271.         
  272.     if (gAppColorTable != NULL)
  273.         DisposeCTable (gAppColorTable);        
  274.     gAppColorTable = NULL;
  275.     
  276.     if (tile)
  277.         if (tileIsResource)
  278.             ReleaseResource (tile);
  279.         else
  280.             DisposeHandle (tile); 
  281.             
  282.     if (grid)
  283.         if (gridIsResource)
  284.             ReleaseResource (grid);
  285.         else
  286.             DisposeHandle (grid);
  287.     return theErr;
  288. }
  289.